#include "GammaNode.h"

#include <QDebug>

#include <math.h>

GammaNode::GammaNode(Node *alpha, Node *beta, bool useInverseScale, double sampleValue, bool isObserved,
                     QString label) :
    Node(useInverseScale ? INVERSE_GAMMA : GAMMA,
        sampleValue, isObserved, label),
    alpha(alpha),
    beta(beta),
    shouldUseInverseScale(useInverseScale)
{
    alpha->addChild(this);
    beta->addChild(this);
}

GammaNode::GammaNode(const GammaNode& other) :
    Node(other),
    alpha(other.alpha),
    beta(other.beta),
    shouldUseInverseScale(other.shouldUseInverseScale)
{

}

GammaNode::~GammaNode()
{

}

double GammaNode::randomSample()
{
    double random = Distribution::sharedInstance()->randomGamma(alpha->getLastSample(), beta->getLastSample());
    if (shouldUseInverseScale)
        return 1.0/random;
    return random;
}

double GammaNode::logLikelihood(double x)
{
    double likelihood =
            Distribution::sharedInstance()->likelihoodGamma(x, alpha->getLastSample(), beta->getLastSample());
    if (shouldUseInverseScale) {
        likelihood = Distribution::sharedInstance()->likelihoodInverseGamma(x, alpha->getLastSample(), beta->getLastSample());
    }

    return log(likelihood);
}
